package main

/*
1. 请实现微信拼手气红包算法，输入红包总额，红包个数，输出红包抽取后的数值序列，算法需考虑公平性、随机性。

题目答案要求：总和为金额数，个数为红包数，单个金额不小于0.01，取两位小数。

 */

import (
	"fmt"
	"math/rand"
	"time"
)

type LuckyRedPacket struct {
	Count int				// 红包个数
	Money int				// 红包总额(以分为单位)
	RemainCount int			// 红包剩余个数
	RemainMoney int			// 红包剩余总额(以分为单位)
	MoneyList []int			// 红包序列
}

func (l *LuckyRedPacket)redPackage() {
	if l.Count == 0 || l.Money == 0{
		panic("红包个数和总额不能为0")
	}

	l.RemainCount = l.Count
	l.RemainMoney = l.Money

	for i := 0;i < l.Count;i++ {
		l.randomRedPacket()
	}
}

func (l *LuckyRedPacket)randomRedPacket() {
	if l.RemainCount == 1 {
		l.MoneyList = append(l.MoneyList,l.RemainMoney)
		l.RemainCount = 0
		l.RemainMoney = 0
		return
	}

	max := l.RemainMoney/l.RemainCount * 2
	rand.Seed(time.Now().UnixNano())
	money := rand.Intn(max)
	for money == 0{
		money = rand.Intn(max)
	}

	l.MoneyList = append(l.MoneyList,money)
	l.RemainMoney -= money
	l.RemainCount--
}

func main() {
	luckyRedPacket := LuckyRedPacket{Count:2,Money:10}
	luckyRedPacket.redPackage()
	fmt.Println("单位分")
	fmt.Println(luckyRedPacket.MoneyList)
}
